package com.base.view

import android.content.Context
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.gyf.barlibrary.ImmersionBar
import com.sh.doctors.cloud.BR
import com.sh.doctors.cloud.R
import com.widget.CommonDialog
import com.widget.LoadingDialog


/**
 * 页面描述：BaseActivity
 *
 * Created by ditclear on 2017/9/27.
 */
abstract class BaseActivity<VB : ViewDataBinding> : AppCompatActivity(), Presenter {

    protected lateinit var mBinding: VB

    protected lateinit var mContext: Context

    protected var autoRefresh = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = DataBindingUtil.setContentView<VB>(this, getLayoutId())
        mBinding.setVariable(BR.presenter, this)
        mBinding.executePendingBindings()
        mBinding.lifecycleOwner = this
        mContext = this
        initView()
        if (autoRefresh) {
            loadData(true)
        }
        ImmersionBar.with(this).statusBarColor(R.color.themeColor).init()
    }


    abstract fun loadData(isRefresh: Boolean)

    abstract fun initView()

    abstract fun getLayoutId(): Int


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> onBackPressed()
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onClick(v: View?) {

    }

    private val loadingDialog by lazy {
        LoadingDialog(mContext)
    }

    fun isLoading() = loadingDialog.isShowing
    fun showLoading(msg: String = mContext.getString(R.string.loading)) = loadingDialog.showMsg(msg)
    fun hideLoading() = loadingDialog.dismiss()


    protected val errorDialog by lazy {
        CommonDialog(mContext).apply {
            content = "提示"
            confirmText = "知道了"
            showCancelBtn = false
            showTimer = false
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        ImmersionBar.with(this).destroy()
    }

}